/*
 * @Date: 2023-02-14 11:04:25
 * @LastEditTime: 2023-05-20 13:52:13
 * @FilePath: /wkw/src/hooks/vantHooks.ts
 * 介绍:
 */
import { i18n } from "@@/language/index";
import {
  showToast,
  showLoadingToast,
  showFailToast,
  showSuccessToast,
  closeToast,
  ToastOptions,
  allowMultipleToast,
} from "vant";
import { showNotify, NotifyOptions } from "vant";
import lockNativeBack from "@@/utils/tools/lockNativeBack";

export const toast = {
  /**
   * * 展示错误提示
   * @param text 提示文字
   * @param config 配置
   * @returns 实例
   */
  error(text: string, config?: ToastOptions) {
    return showFailToast({
      ...config,
      message: text,
    });
  },
  /**
   * * 展示成功提示
   * @param text 提示文字
   * @param config 配置
   * @returns 实例
   */
  success(text: string, config?: ToastOptions) {
    return showSuccessToast({
      ...config,
      message: text,
    });
  },
  /**
   * * 展示加载中提示
   * @param text 提示文字
   * @param config 配置
   * @returns 实例
   */
  loading(text?: string, config?: ToastOptions) {
    //@ts-ignore
    const { t } = i18n.global;
    //@ts-ignore
    text ??= t("qing-shao-hou");
    lockNativeBack.lock();
    if (config?.duration) lockNativeBack.timingUnLock(config.duration);
    return showLoadingToast({
      duration: 0,
      overlay: true,
      ...config,
      message: text,
    });
  },
  /**
   * * 展示文本提示
   * @param text 提示文字
   * @param config 配置
   * @returns 实例
   */
  text(text: string, config?: ToastOptions) {
    return showToast({
      ...config,
      message: text,
    });
  },
  /**
   * * 关闭提示
   */
  close() {
    lockNativeBack.unLock();
    return closeToast();
  },
  allowMultipleToast,
};

export const notify = {
  /**主要通知 */
  primary(message: string, config?: NotifyOptions) {
    return showNotify({ message, duration: 3000, ...config, type: "primary" });
  },
  /**成功通知 */
  success(message: string, config?: NotifyOptions) {
    return showNotify({ message, duration: 3000, ...config, type: "success" });
  },

  /**失败通知 */
  error(message: string, config?: NotifyOptions) {
    return showNotify({ message, duration: 3000, ...config, type: "danger" });
  },

  /**警告通知 */
  warn(message: string, config?: NotifyOptions) {
    return showNotify({ message, duration: 3000, ...config, type: "warning" });
  },
};
